/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import Beacon from '../Beacon';
import BeaconParser from '../BeaconParser';
import HexUtil from './HexUtil';
class EddystoneTelemetryAccessor{
  public readonly TAG:string="EddystoneTLMAccessor";

  public getTelemetryBytes(beacon:Beacon):Array<number>{
    if(beacon.getExtraDataFields().length >= 5){
      var telemetryBeacon:Beacon=new Beacon.Builder().setDataFields(beacon.getExtraDataFields()).build();

      var telemetryParser:BeaconParser=new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT);
      var telemetryBytes:Array<number> =telemetryParser.getBeaconAdvertisementData(telemetryBeacon);
      console.log(this.TAG, "Rehydrated telemetry bytes are :" + this.byteArrayToString(telemetryBytes));
      return telemetryBytes;
    }
    else{
      return null;
    }
  }

  public byteArrayToString(bytes:Array<number>):string{
    let sb = new Array();
    for (var i:number = 0; i < bytes.length; i++) {
      sb.push(HexUtil.formatHexString(new Int8Array(bytes[i])));
      sb.push(" ");
    }
    return sb.join('').trim().toString();
  }
}
export default EddystoneTelemetryAccessor;